﻿using InfrastructureBase.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Workflow.Api.Controllers
{
    /// <summary>
    /// mongodb数据库测试接口
    /// </summary>s
    [Route("api/[controller]")]
    [ApiController]
    public class MongoTestController : ControllerBase
    {
        private readonly IMongoConnection mongoConnection;

        public MongoTestController(IMongoConnection _mongoConnection)
        {
            mongoConnection = _mongoConnection;
        }

        /// <summary>
        /// 获取mongo数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet,Route("database")]
        public IActionResult GetDataBase()
        {
            IMongoDatabase mongoDatabase= mongoConnection.GetDataBase();
            return Ok(mongoDatabase);
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("collection")]
        public IActionResult GetCollections()
        {
            IMongoCollection<object> mongoDatabase = mongoConnection.GetCollections<object>();
            return Ok(mongoDatabase);
        }

        /// <summary>
        /// 新增集合
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("")]
        public IActionResult AddCollections()
        {
            object item=new
            {
                userName="张三",
                age=12,
                sex=1,
            };
            mongoConnection.AddEntityToCollection(item,"logmanage1");
            return Ok();
        }

        /// <summary>
        /// 从集合中删除满足条件的数据
        /// </summary>
        /// <returns></returns>
        [HttpDelete, Route("")]
        public IActionResult DeleteEntityToCollection()
        {
            //mongoConnection.DeleteEntityToCollection(, "logmanage1");
            return Ok();
        }

       /// <summary>
       /// 获取全部
       /// </summary>
       /// <returns></returns>
        [HttpGet,Route("")]
        public  IActionResult GetEntitiesV1()
        {
            //IEnumerable<object> objects = mongoConnection.GetEntitiesV1<object>(Builders<object>.Filter.Empty);
            IEnumerable<object> objects = mongoConnection.GetEntities<object>(_ => true);
            return Ok(objects);
            
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="formFiles"></param>
        /// <returns></returns>
        [HttpPost,Route("file")]
        public async Task<IActionResult> UploadFileAsync([FromForm]IFormFileCollection formFiles)
        {
            List<ObjectId> objectIds = await mongoConnection.UploadFileToCollectionAsync(formFiles);
            return Ok(objectIds);
        }

        /// <summary>
        /// 根据标识查询文件
        /// </summary>
        /// <param name="objectId"></param>
        /// <returns></returns>
        [HttpGet,Route("file")]
        public async Task<ObjectId> GetFileByObjectIdAsync(string objectId)
        {
            //string name = AttributeExtention.GetValue(ReadFileType.Byte);
            return await mongoConnection.GetFileByObjectIdAsync(ObjectId.Parse(objectId));
            //return await Task.FromResult(ObjectId.Parse(objectId));
        }

        public enum ReadFileType:int
        { 
            File,
            Byte,
        }
    }
}
